<?php
	session_start(); 

/*™*******************************************************************************************™*/
	
	// include script
	require("./inc/config-system.inc.php");	
	require("./inc/config-db.inc.php");
	require("./inc/fm.inc.php");
	require("./class/adodb/adodb.inc.php");
	require("./class/validate/validate.inc.php");

/*™*******************************************************************************************™*/
/*-----> TRUNCATE TABLE
/*™*******************************************************************************************™*/

	if (isset($_POST["BTTtruncate"])) {

		//-----> connection database
		$objConn_imp = ADONewConnection(DB_DRIVER);
		$objConn_imp->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME_IMPORT);
		//$objConn_imp->debug = TRUE;

		$sqlCommand = "TRUNCATE TABLE tbl_ar";
		$objConn_imp->Execute($sqlCommand);

		$sqlCommand = "TRUNCATE TABLE tbl_ap";
		$objConn_imp->Execute($sqlCommand);

		$sqlCommand = "TRUNCATE TABLE tbl_adj";
		$objConn_imp->Execute($sqlCommand);

		$location = "./auto_post_form.php";
		
		echo "{'POSTING_SUCCESS':'Truncate Table AR, AP, ADJUST Success',\n 'POSTING_REDIRECT':'" . rawurlencode($location) . "'}";
		exit();
									
	}
	
/*™*******************************************************************************************™*/
	
	// validate data input
    
    // set rule for each input field
    $position = 0;
    $inputLabel[] = "Document Type";
    $ruleElement["SELtype"][0] = array
        (
            "checkNotSelect" => $inputLabel[$position]
        );

    $position = $position + 1;
    $inputLabel[] = "Document No";
    $ruleElement["TXTdoc_no_from"][0] = array
        (
            "checkNotEmpty" => $inputLabel[$position]
        );

    // validate rule header                                          
    $errorMessageJSON = validateElementAdminAccount($ruleElement, 0); // see in fm.inc.php
    
    if (!empty($errorMessageJSON)) {
        echo $errorMessageJSON; // return json format
        exit();
    } else {
        $inputLabel = array();
        $ruleElement = array();
        $position = 0;
    }      

/*™*******************************************************************************************™*/


	if ($_POST["TXTdoc_no_from"][0] != "") {
		$doc_no_from = $_POST["TXTdoc_no_from"][0];
	}

	if ($_POST["TXTdoc_no_to"][0] != "") {
		$doc_no_to = $_POST["TXTdoc_no_to"][0];
	} else {
		$doc_no_to = $_POST["TXTdoc_no_from"][0];
	}

	if ($_POST["TXTdoc_no_from"][0] > $_POST["TXTdoc_no_to"][0] && $_POST["TXTdoc_no_to"][0] != "") {
		echo "{'POSTING_ERROR':'Document No. from NOT MORE THAN Document No. to'}";
		exit();
	}
	
/*™*******************************************************************************************™*/
/*-----> AR
/*™*******************************************************************************************™*/
	
	switch ($_POST["SELtype"][0]) {

		case "AR":
		
			//-----> connection database for import data
			$objConn_imp = ADONewConnection(DB_DRIVER);
			$objConn_imp->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME_IMPORT);
			//$objConn_imp->debug = TRUE;
		
			// keep AR data into result array
			$result_data = array();
			$result_data_temp = array();

			// select AR data into result array
			$sqlCommand = "SELECT * FROM tbl_ar WHERE (ar_docno BETWEEN " . $doc_no_from . " AND " . $doc_no_to . ") ORDER BY ar_docno";
			$recordset = $objConn_imp->Execute($sqlCommand);
		
			while (!$recordset->EOF) {
				
				$result_data_temp["ar_docdate"] = $recordset->fields["ar_docdate"];
				$result_data_temp["ar_account_desc"] = $recordset->fields["ar_account_desc"];
				$result_data_temp["ar_description"] = $recordset->fields["ar_description"];
				$result_data_temp["ar_quantity"] = $recordset->fields["ar_quantity"];
				$result_data_temp["ar_price_unit"] = $recordset->fields["ar_price_unit"];
				$result_data_temp["ar_payer"] = $recordset->fields["ar_payer"];
				$result_data_temp["ar_remark"] = $recordset->fields["ar_remark"];
				$result_data_temp["ar_account_cash"] = $recordset->fields["ar_account_cash"];
				$result_data_temp["ar_amount_cash"] = $recordset->fields["ar_amount_cash"];
				$result_data_temp["ar_account_saving"] = $recordset->fields["ar_account_saving"];
				$result_data_temp["ar_amount_saving"] = $recordset->fields["ar_amount_saving"];
				$result_data_temp["ar_account_current"] = $recordset->fields["ar_account_current"];
				$result_data_temp["ar_amount_current"] = $recordset->fields["ar_amount_current"];
				$result_data_temp["ar_account_pledge"] = $recordset->fields["ar_account_pledge"];
				$result_data_temp["ar_amount_pledge"] = $recordset->fields["ar_amount_pledge"];
				$result_data_temp["ar_docno"] = $recordset->fields["ar_docno"];
				$result_data_temp["status"] = $recordset->fields["status"];
				$result_data_temp["deleted"] = $recordset->fields["deleted"];
				$result_data_temp["created_date"] = $recordset->fields["created_date"];
				$result_data_temp["modified_date"] = $recordset->fields["modified_date"];
				$result_data_temp["deleted_date"] = $recordset->fields["deleted_date"];
		
				array_push($result_data, $result_data_temp);
				
				$recordset->MoveNext();
						
			}
		
			unset($result_data_temp); // clear array
		
			//-----> connection database for insert data
			$objConn = ADONewConnection(DB_DRIVER);
			$objConn->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
			//$objConn->debug = TRUE;

			// loop check document that already posted
			for ($index = 0; $index <= count($result_data) - 1; $index++) {    
			
				$sqlCommand = "SELECT * FROM tbl_ar WHERE ar_refdoc = " . $result_data[$index]["ar_docno"] . " LIMIT 1";  
				$recordset = $objConn->Execute($sqlCommand);
				
				if ($recordset->RecordCount() == 1) {
					echo "{'POSTING_ERROR':'AR Document No. " . $result_data[$index]["ar_docno"] . " Already Posted'}";
					exit();
				}
				
			} 
					
			// insert item in AR document
			for ($index = 0; $index <= count($result_data) - 1; $index++) {    
		
				// get running no.
				$sqlCommand = "SELECT * FROM tbl_runing_doc WHERE run_type = 'AR' LIMIT 1";  
				$recordset = $objConn->Execute($sqlCommand);
				while (!$recordset->EOF) {
					$run_no = $recordset->fields["run_no"];
					$recordset->MoveNext();
				}
		
				// execute the query and get empty recordset
				$sqlCommand = "SELECT * FROM tbl_ar WHERE ar_id = -1";  
				$recordset = $objConn->Execute($sqlCommand);
				
				if ($result_data[$index]["ar_amount_cash"] != 0.00) {	// CASH
		
					$record = array(); 
					
					$record["ar_refdoc"] = $result_data[$index]["ar_docno"]; 
					$record["ar_docdate"] = $result_data[$index]["ar_docdate"]; 
					$record["ar_postkey"] = "01"; 
					$record["ar_account"] = $result_data[$index]["ar_account_cash"]; 
					$record["ar_description"] = $result_data[$index]["ar_remark"] . "  " . $result_data[$index]["ar_payer"]; 
					$record["ar_quantity"] = $result_data[$index]["ar_quantity"]; 
					$record["ar_price_unit"] = $result_data[$index]["ar_price_unit"]; 
					$record["ar_amount"] = $result_data[$index]["ar_amount_cash"];
					$record["ar_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					$record["ar_refdoc"] = $result_data[$index]["ar_docno"]; 
					$record["ar_docdate"] = $result_data[$index]["ar_docdate"]; 
					$record["ar_postkey"] = "02"; 
					$record["ar_account"] = $result_data[$index]["ar_account_desc"]; 
					$record["ar_description"] = $result_data[$index]["ar_remark"] . "  " . $result_data[$index]["ar_payer"];; 
					$record["ar_quantity"] = $result_data[$index]["ar_quantity"]; 
					$record["ar_price_unit"] = $result_data[$index]["ar_price_unit"]; 
					$record["ar_amount"] = $result_data[$index]["ar_amount_cash"];
					$record["ar_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
				
				if ($result_data[$index]["ar_amount_saving"] != 0.00) { // SAVING
		
					$record = array(); 
					
					$record["ar_refdoc"] = $result_data[$index]["ar_docno"]; 
					$record["ar_docdate"] = $result_data[$index]["ar_docdate"]; 
					$record["ar_postkey"] = "01"; 
					$record["ar_account"] = $result_data[$index]["ar_account_saving"]; 
					$record["ar_description"] = $result_data[$index]["ar_remark"] . "  " . $result_data[$index]["ar_payer"];; 
					$record["ar_quantity"] = $result_data[$index]["ar_quantity"]; 
					$record["ar_price_unit"] = $result_data[$index]["ar_price_unit"]; 
					$record["ar_amount"] = $result_data[$index]["ar_amount_saving"];
					$record["ar_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					$record["ar_refdoc"] = $result_data[$index]["ar_docno"]; 
					$record["ar_docdate"] = $result_data[$index]["ar_docdate"]; 
					$record["ar_postkey"] = "02"; 
					$record["ar_account"] = $result_data[$index]["ar_account_desc"]; 
					$record["ar_description"] = $result_data[$index]["ar_remark"] . "  " . $result_data[$index]["ar_payer"];; 
					$record["ar_quantity"] = $result_data[$index]["ar_quantity"]; 
					$record["ar_price_unit"] = $result_data[$index]["ar_price_unit"]; 
					$record["ar_amount"] = $result_data[$index]["ar_amount_saving"];
					$record["ar_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
		
				if ($result_data[$index]["ar_amount_current"] != 0.00) { // CURRENT
		
					$record = array(); 
					
					$record["ar_refdoc"] = $result_data[$index]["ar_docno"]; 
					$record["ar_docdate"] = $result_data[$index]["ar_docdate"]; 
					$record["ar_postkey"] = "01"; 
					$record["ar_account"] = $result_data[$index]["ar_account_current"]; 
					$record["ar_description"] = $result_data[$index]["ar_remark"] . "  " . $result_data[$index]["ar_payer"];; 
					$record["ar_quantity"] = $result_data[$index]["ar_quantity"]; 
					$record["ar_price_unit"] = $result_data[$index]["ar_price_unit"]; 
					$record["ar_amount"] = $result_data[$index]["ar_amount_current"];
					$record["ar_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					$record["ar_refdoc"] = $result_data[$index]["ar_docno"]; 
					$record["ar_docdate"] = $result_data[$index]["ar_docdate"]; 
					$record["ar_postkey"] = "02"; 
					$record["ar_account"] = $result_data[$index]["ar_account_desc"]; 
					$record["ar_description"] = $result_data[$index]["ar_remark"] . "  " . $result_data[$index]["ar_payer"];; 
					$record["ar_quantity"] = $result_data[$index]["ar_quantity"]; 
					$record["ar_price_unit"] = $result_data[$index]["ar_price_unit"]; 
					$record["ar_amount"] = $result_data[$index]["ar_amount_current"];
					$record["ar_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
		
				if ($result_data[$index]["ar_amount_pledge"] != 0.00) { // PLEDGE
		
					$record = array(); 
					
					$record["ar_refdoc"] = $result_data[$index]["ar_docno"]; 
					$record["ar_docdate"] = $result_data[$index]["ar_docdate"]; 
					$record["ar_postkey"] = "01"; 
					$record["ar_account"] = $result_data[$index]["ar_account_pledge"]; 
					$record["ar_description"] = $result_data[$index]["ar_remark"] . "  " . $result_data[$index]["ar_payer"];; 
					$record["ar_quantity"] = $result_data[$index]["ar_quantity"]; 
					$record["ar_price_unit"] = $result_data[$index]["ar_price_unit"]; 
					$record["ar_amount"] = $result_data[$index]["ar_amount_pledge"];
					$record["ar_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					$record["ar_refdoc"] = $result_data[$index]["ar_docno"]; 
					$record["ar_docdate"] = $result_data[$index]["ar_docdate"]; 
					$record["ar_postkey"] = "02"; 
					$record["ar_account"] = $result_data[$index]["ar_account_desc"]; 
					$record["ar_description"] = $result_data[$index]["ar_remark"] . "  " . $result_data[$index]["ar_payer"];; 
					$record["ar_quantity"] = $result_data[$index]["ar_quantity"]; 
					$record["ar_price_unit"] = $result_data[$index]["ar_price_unit"]; 
					$record["ar_amount"] = $result_data[$index]["ar_amount_pledge"];
					$record["ar_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
		
				// update running no
				$sqlCommand = "SELECT * FROM tbl_runing_doc WHERE run_type = 'AR'";  
				$recordset = $objConn->Execute($sqlCommand);
			
				// record data to update
				$record = array(); 
			
				$record["run_no"] = $run_no + 1; 
				
				$record["created_date"] = time(); 
				$record["modified_date"] = time(); 
				
				$sqlCommand = $objConn->GetUpdateSQL($recordset, $record);
				$objConn->Execute($sqlCommand); 
				
			} // for
			
			break;

/*™*******************************************************************************************™*/
/*-----> AP
/*™*******************************************************************************************™*/

		case "AP":
		
			//-----> connection database for import data
			$objConn_imp = ADONewConnection(DB_DRIVER);
			$objConn_imp->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME_IMPORT);
			//$objConn_imp->debug = TRUE;
		
			// keep AP data into result array
			$result_data = array();
			$result_data_temp = array();
		
			// select AP data into result array
			$sqlCommand = "SELECT * FROM tbl_ap WHERE (ap_docno BETWEEN " . $doc_no_from . " AND " . $doc_no_to . ") ORDER BY ap_docno";
			$recordset = $objConn_imp->Execute($sqlCommand);
		
			while (!$recordset->EOF) {
				
				$result_data_temp["ap_docdate"] = $recordset->fields["ap_docdate"];
				$result_data_temp["ap_account_desc"] = $recordset->fields["ap_account_desc"];
				$result_data_temp["ap_description"] = $recordset->fields["ap_description"];
				$result_data_temp["ap_quantity"] = $recordset->fields["ap_quantity"];
				$result_data_temp["ap_price_unit"] = $recordset->fields["ap_price_unit"];
				$result_data_temp["ap_payer"] = $recordset->fields["ap_payer"];
				$result_data_temp["ap_remark"] = $recordset->fields["ap_remark"];
				$result_data_temp["ap_account_cash"] = $recordset->fields["ap_account_cash"];
				$result_data_temp["ap_amount_cash"] = $recordset->fields["ap_amount_cash"];
				$result_data_temp["ap_account_saving"] = $recordset->fields["ap_account_saving"];
				$result_data_temp["ap_amount_saving"] = $recordset->fields["ap_amount_saving"];
				$result_data_temp["ap_account_current"] = $recordset->fields["ap_account_current"];
				$result_data_temp["ap_amount_current"] = $recordset->fields["ap_amount_current"];
				$result_data_temp["ap_account_pledge"] = $recordset->fields["ap_account_pledge"];
				$result_data_temp["ap_amount_pledge"] = $recordset->fields["ap_amount_pledge"];
				$result_data_temp["ap_docno"] = $recordset->fields["ap_docno"];
				$result_data_temp["status"] = $recordset->fields["status"];
				$result_data_temp["deleted"] = $recordset->fields["deleted"];
				$result_data_temp["created_date"] = $recordset->fields["created_date"];
				$result_data_temp["modified_date"] = $recordset->fields["modified_date"];
				$result_data_temp["deleted_date"] = $recordset->fields["deleted_date"];
		
				array_push($result_data, $result_data_temp);
				
				$recordset->MoveNext();
						
			}
		
			unset($result_data_temp); // clear array
		
			//-----> connection database for insert data
			$objConn = ADONewConnection(DB_DRIVER);
			$objConn->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
			//$objConn->debug = TRUE;
			
			// loop check document that already posted
			for ($index = 0; $index <= count($result_data) - 1; $index++) {    
			
				$sqlCommand = "SELECT * FROM tbl_ap WHERE ap_refdoc = " . $result_data[$index]["ap_docno"] . " LIMIT 1";  
				$recordset = $objConn->Execute($sqlCommand);
				
				if ($recordset->RecordCount() == 1) {
					echo "{'POSTING_ERROR':'AP Document No. " . $result_data[$index]["ap_docno"] . " Already Posted'}";
					exit();
				}
				
			} 
			
			// insert item in AP document
			for ($index = 0; $index <= count($result_data) - 1; $index++) {    
		
				// get running no.
				$sqlCommand = "SELECT * FROM tbl_runing_doc WHERE run_type = 'AP' LIMIT 1";  
				$recordset = $objConn->Execute($sqlCommand);
				while (!$recordset->EOF) {
					$run_no = $recordset->fields["run_no"];
					$recordset->MoveNext();
				}
		
				// execute the query and get empty recordset
				$sqlCommand = "SELECT * FROM tbl_ap WHERE ap_id = -1";  
				$recordset = $objConn->Execute($sqlCommand);
				
/*				if ($result_data[$index]["ap_amount_cash"] != 0.00) {	// CASH
		
					$record = array(); 
					
					$record["ap_refdoc"] = $result_data[$index]["ap_docno"]; 
					$record["ap_docdate"] = $result_data[$index]["ap_docdate"]; 
					$record["ap_postkey"] = "02"; 
					$record["ap_account"] = $result_data[$index]["ap_account_cash"]; 
					$record["ap_description"] = $result_data[$index]["ap_remark"] . "  " . $result_data[$index]["ap_payer"]; 
					$record["ap_quantity"] = $result_data[$index]["ap_quantity"]; 
					$record["ap_price_unit"] = $result_data[$index]["ap_price_unit"]; 
					$record["ap_amount"] = $result_data[$index]["ap_amount_cash"];
					$record["ap_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					$record["ap_refdoc"] = $result_data[$index]["ap_docno"]; 
					$record["ap_docdate"] = $result_data[$index]["ap_docdate"]; 
					$record["ap_postkey"] = "01"; 
					$record["ap_account"] = $result_data[$index]["ap_account_desc"]; 
					$record["ap_description"] = $result_data[$index]["ap_remark"] . "  " . $result_data[$index]["ap_payer"];; 
					$record["ap_quantity"] = $result_data[$index]["ap_quantity"]; 
					$record["ap_price_unit"] = $result_data[$index]["ap_price_unit"]; 
					$record["ap_amount"] = $result_data[$index]["ap_amount_cash"];
					$record["ap_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
*/				
				if ($result_data[$index]["ap_amount_saving"] != 0.00) { // SAVING
		
					$record = array(); 
					
					$record["ap_refdoc"] = $result_data[$index]["ap_docno"]; 
					$record["ap_docdate"] = $result_data[$index]["ap_docdate"]; 
					$record["ap_postkey"] = "02"; 
					$record["ap_account"] = $result_data[$index]["ap_account_saving"]; 
					$record["ap_description"] = $result_data[$index]["ap_remark"] . "  " . $result_data[$index]["ap_payer"];; 
					$record["ap_quantity"] = $result_data[$index]["ap_quantity"]; 
					$record["ap_price_unit"] = $result_data[$index]["ap_price_unit"]; 
					$record["ap_amount"] = $result_data[$index]["ap_amount_saving"];
					$record["ap_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					$record["ap_refdoc"] = $result_data[$index]["ap_docno"]; 
					$record["ap_docdate"] = $result_data[$index]["ap_docdate"]; 
					$record["ap_postkey"] = "01"; 
					$record["ap_account"] = $result_data[$index]["ap_account_desc"]; 
					$record["ap_description"] = $result_data[$index]["ap_remark"] . "  " . $result_data[$index]["ap_payer"];; 
					$record["ap_quantity"] = $result_data[$index]["ap_quantity"]; 
					$record["ap_price_unit"] = $result_data[$index]["ap_price_unit"]; 
					$record["ap_amount"] = $result_data[$index]["ap_amount_saving"];
					$record["ap_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
		
				if ($result_data[$index]["ap_amount_current"] != 0.00) { // CURRENT
		
					$record = array(); 
					
					$record["ap_refdoc"] = $result_data[$index]["ap_docno"]; 
					$record["ap_docdate"] = $result_data[$index]["ap_docdate"]; 
					$record["ap_postkey"] = "02"; 
					$record["ap_account"] = $result_data[$index]["ap_account_current"]; 
					$record["ap_description"] = $result_data[$index]["ap_remark"] . "  " . $result_data[$index]["ap_payer"];; 
					$record["ap_quantity"] = $result_data[$index]["ap_quantity"]; 
					$record["ap_price_unit"] = $result_data[$index]["ap_price_unit"]; 
					$record["ap_amount"] = $result_data[$index]["ap_amount_current"];
					$record["ap_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					$record["ap_refdoc"] = $result_data[$index]["ap_docno"]; 
					$record["ap_docdate"] = $result_data[$index]["ap_docdate"]; 
					$record["ap_postkey"] = "01"; 
					$record["ap_account"] = $result_data[$index]["ap_account_desc"]; 
					$record["ap_description"] = $result_data[$index]["ap_remark"] . "  " . $result_data[$index]["ap_payer"];; 
					$record["ap_quantity"] = $result_data[$index]["ap_quantity"]; 
					$record["ap_price_unit"] = $result_data[$index]["ap_price_unit"]; 
					$record["ap_amount"] = $result_data[$index]["ap_amount_current"];
					$record["ap_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
		
/*				if ($result_data[$index]["ap_amount_pledge"] != 0.00) { // PLEDGE
		
					$record = array(); 
					
					$record["ap_refdoc"] = $result_data[$index]["ap_docno"]; 
					$record["ap_docdate"] = $result_data[$index]["ap_docdate"]; 
					$record["ap_postkey"] = "02"; 
					$record["ap_account"] = $result_data[$index]["ap_account_pledge"]; 
					$record["ap_description"] = $result_data[$index]["ap_remark"] . "  " . $result_data[$index]["ap_payer"];; 
					$record["ap_quantity"] = $result_data[$index]["ap_quantity"]; 
					$record["ap_price_unit"] = $result_data[$index]["ap_price_unit"]; 
					$record["ap_amount"] = $result_data[$index]["ap_amount_pledge"];
					$record["ap_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					$record["ap_refdoc"] = $result_data[$index]["ap_docno"]; 
					$record["ap_docdate"] = $result_data[$index]["ap_docdate"]; 
					$record["ap_postkey"] = "01"; 
					$record["ap_account"] = $result_data[$index]["ap_account_desc"]; 
					$record["ap_description"] = $result_data[$index]["ap_remark"] . "  " . $result_data[$index]["ap_payer"];; 
					$record["ap_quantity"] = $result_data[$index]["ap_quantity"]; 
					$record["ap_price_unit"] = $result_data[$index]["ap_price_unit"]; 
					$record["ap_amount"] = $result_data[$index]["ap_amount_pledge"];
					$record["ap_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
*/		
				// update running no
				$sqlCommand = "SELECT * FROM tbl_runing_doc WHERE run_type = 'AP'";  
				$recordset = $objConn->Execute($sqlCommand);
			
				// record data to update
				$record = array(); 
			
				$record["run_no"] = $run_no + 1; 
				
				$record["created_date"] = time(); 
				$record["modified_date"] = time(); 
				
				$sqlCommand = $objConn->GetUpdateSQL($recordset, $record);
				$objConn->Execute($sqlCommand); 
				
			} // for
			
			break;

/*™*******************************************************************************************™*/
/*-----> ADJ
/*™*******************************************************************************************™*/

		case "ADJ":

			//-----> connection database for import data
			$objConn_imp = ADONewConnection(DB_DRIVER);
			$objConn_imp->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME_IMPORT);
			//$objConn_imp->debug = TRUE;
		
			// keep ADJ data into result array
			$result_data = array();
			$result_data_temp = array();

			// select ADJ data into result array
			$sqlCommand = "SELECT * FROM tbl_adj WHERE (adj_docno BETWEEN " . $doc_no_from . " AND " . $doc_no_to . ") ORDER BY adj_docno";
			$recordset = $objConn_imp->Execute($sqlCommand);
		
			while (!$recordset->EOF) {
				
				$result_data_temp["adj_docdate"] = $recordset->fields["adj_docdate"];
				$result_data_temp["adj_account_desc"] = $recordset->fields["adj_account_desc"];
				$result_data_temp["adj_description"] = $recordset->fields["adj_description"];
				$result_data_temp["adj_quantity"] = $recordset->fields["adj_quantity"];
				$result_data_temp["adj_price_unit"] = $recordset->fields["adj_price_unit"];
				$result_data_temp["adj_payer"] = $recordset->fields["adj_payer"];
				$result_data_temp["adj_remark"] = $recordset->fields["adj_remark"];
				$result_data_temp["adj_account_shuttlecock"] = $recordset->fields["adj_account_shuttlecock"];
				$result_data_temp["adj_amount_shuttlecock"] = $recordset->fields["adj_amount_shuttlecock"];
				$result_data_temp["adj_account_softdrink"] = $recordset->fields["adj_account_softdrink"];
				$result_data_temp["adj_amount_softdrink"] = $recordset->fields["adj_amount_softdrink"];
				$result_data_temp["adj_account_mineral"] = $recordset->fields["adj_account_mineral"];
				$result_data_temp["adj_amount_mineral"] = $recordset->fields["adj_amount_mineral"];
				$result_data_temp["adj_account_water"] = $recordset->fields["adj_account_water"];
				$result_data_temp["adj_amount_water"] = $recordset->fields["adj_amount_water"];
				$result_data_temp["adj_account_loan"] = $recordset->fields["adj_account_loan"];
				$result_data_temp["adj_amount_loan"] = $recordset->fields["adj_amount_loan"];
				$result_data_temp["adj_account_advance"] = $recordset->fields["adj_account_advance"];
				$result_data_temp["adj_amount_advance"] = $recordset->fields["adj_amount_advance"];
				$result_data_temp["status"] = $recordset->fields["status"];
				$result_data_temp["deleted"] = $recordset->fields["deleted"];
				$result_data_temp["created_date"] = $recordset->fields["created_date"];
				$result_data_temp["modified_date"] = $recordset->fields["modified_date"];
				$result_data_temp["deleted_date"] = $recordset->fields["deleted_date"];
		
				array_push($result_data, $result_data_temp);
				
				$recordset->MoveNext();
						
			}
		
			unset($result_data_temp); // clear array
		
			//-----> connection database for insert data
			$objConn = ADONewConnection(DB_DRIVER);
			$objConn->Connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
			//$objConn->debug = TRUE;

			// loop check document that already posted
/*			for ($index = 0; $index <= count($result_data) - 1; $index++) {    
			
				$sqlCommand = "SELECT * FROM tbl_adj WHERE adj_refdoc = " . $result_data[$index]["adj_docno"] . " LIMIT 1";  
				$recordset = $objConn->Execute($sqlCommand);
				
				if ($recordset->RecordCount() == 1) {
					echo "{'POSTING_ERROR':'ADJ Document No. " . $result_data[$index]["adj_docno"] . " Already Posted'}";
					exit();
				}
				
			} 
*/					
			// insert item in ADJ document
			for ($index = 0; $index <= count($result_data) - 1; $index++) {    
		
				// get running no.
				$sqlCommand = "SELECT * FROM tbl_runing_doc WHERE run_type = 'AD' LIMIT 1";  
				$recordset = $objConn->Execute($sqlCommand);
				while (!$recordset->EOF) {
					$run_no = $recordset->fields["run_no"];
					$recordset->MoveNext();
				}
		
				// execute the query and get empty recordset
				$sqlCommand = "SELECT * FROM tbl_adj WHERE adj_id = -1";  
				$recordset = $objConn->Execute($sqlCommand);
				
				if ($result_data[$index]["adj_amount_shuttlecock"] != 0.00) {	// SHUTTERCOCK
		
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "01"; 
					$record["adj_account"] = $result_data[$index]["adj_account_shuttlecock"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"]; 
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_shuttlecock"];
					$record["adj_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "02"; 
					$record["adj_account"] = $result_data[$index]["adj_account_desc"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"];
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_shuttlecock"];
					$record["adj_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
				
				if ($result_data[$index]["adj_amount_softdrink"] != 0.00) { // SOFTDRINK
		
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "01"; 
					$record["adj_account"] = $result_data[$index]["adj_account_softdrink"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"]; 
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_softdrink"];
					$record["adj_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "02"; 
					$record["adj_account"] = $result_data[$index]["adj_account_desc"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"];; 
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_softdrink"];
					$record["adj_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
		
				if ($result_data[$index]["adj_amount_mineral"] != 0.00) { // MINERAL
		
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "01"; 
					$record["adj_account"] = $result_data[$index]["adj_account_mineral"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"]; 
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_mineral"];
					$record["adj_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "02"; 
					$record["adj_account"] = $result_data[$index]["adj_account_desc"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"]; 
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_mineral"];
					$record["adj_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
		
				if ($result_data[$index]["adj_amount_water"] != 0.00) { // WATER
		
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "01"; 
					$record["adj_account"] = $result_data[$index]["adj_account_water"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"];;
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_water"];
					$record["adj_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "02"; 
					$record["adj_account"] = $result_data[$index]["adj_account_desc"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"]; 
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_water"];
					$record["adj_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}

				//---------------------->
				
				if ($result_data[$index]["adj_amount_loan"] != 0.00) { // LOAN
		
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "02"; 
					$record["adj_account"] = $result_data[$index]["adj_account_loan"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"];
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_loan"];
					$record["adj_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "01"; 
					$record["adj_account"] = $result_data[$index]["adj_account_desc"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"]; 
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_loan"];
					$record["adj_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}

				if ($result_data[$index]["adj_amount_advance"] != 0.00) { // ADVANCE
		
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "02"; 
					$record["adj_account"] = $result_data[$index]["adj_account_advance"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"];
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_advance"];
					$record["adj_docno"] = $run_no;; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
		
					//---->
					$record = array(); 
					
					//$record["adj_refdoc"] = $result_data[$index]["adj_docno"]; 
					//$record["adj_docreverse"] = $result_data[$index]["adj_docreverse"]; 
					$record["adj_docdate"] = $result_data[$index]["adj_docdate"]; 
					$record["adj_postkey"] = "01"; 
					$record["adj_account"] = $result_data[$index]["adj_account_desc"]; 
					$record["adj_description"] = $result_data[$index]["adj_remark"] . "  " . $result_data[$index]["adj_payer"]; 
					$record["adj_quantity"] = $result_data[$index]["adj_quantity"]; 
					$record["adj_price_unit"] = $result_data[$index]["adj_price_unit"]; 
					$record["adj_amount"] = $result_data[$index]["adj_amount_advance"];
					$record["adj_docno"] = $run_no; 
					$record["created_date"] = time(); 
					$record["modified_date"] = time(); 
			
					$sqlCommand = $objConn->GetInsertSQL($recordset, $record);
					$objConn->Execute($sqlCommand); 
					
				}
		
				// update running no
				$sqlCommand = "SELECT * FROM tbl_runing_doc WHERE run_type = 'AD'";  
				$recordset = $objConn->Execute($sqlCommand);
			
				// record data to update
				$record = array(); 
			
				$record["run_no"] = $run_no + 1; 
				
				$record["created_date"] = time(); 
				$record["modified_date"] = time(); 
				
				$sqlCommand = $objConn->GetUpdateSQL($recordset, $record);
				$objConn->Execute($sqlCommand); 
				
			} // for
	
			break;
		
	}
	
/*™*******************************************************************************************™*/

	$location = "./auto_post_form.php";
	
	echo "{'POSTING_SUCCESS':'Posting Document " . $_POST["SELtype"][0] . " Success',\n 'POSTING_REDIRECT':'" . rawurlencode($location) . "'}";
	exit();
	
/*™*******************************************************************************************™*/
?>